﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.Contract.DataContract;
using Core.DAO;
using Data.BO;
using www.Models;

namespace Controllerss
{
   public class CategoryController
   {
       #region Value
       private readonly CategoryRefBO _categoryBo = new CategoryRefBO();
       private readonly CategoryValueBO _categoryValueBo = new CategoryValueBO();
       #endregion

       #region Ham lay du lieu

       public IQueryable<RelaxCategoryDataContract> GetCategoryDataContracts()
       {
           using (var scope = new TransactionScope())
           {
               var lReturn = _categoryBo.GetCategoryDataContracts();
               scope.Complete();
               return lReturn;
           }
       }
       #endregion

       #region Add

       public long Add(List<RelaxCategoryDataContract> categoryDataContract)
       {
           using (var scope = new TransactionScope())
           {
               var iD = 0;
               var category = new CATEGORY_REF
               {
                   typeid = Convert.ToInt32(categoryDataContract[0].Type),
                   ord = Convert.ToInt32(categoryDataContract[0].Ord),
                   active = Convert.ToInt32(categoryDataContract[0].Active)
               };

               _categoryBo.Add(category);
             
               var first =  _categoryBo.GetCategoryDataContract().OrderByDescending(o => o.Id).FirstOrDefault();
               if (first != null)
               {
                   iD = Convert.ToInt32(first.Id);
                   
                   foreach (var item in categoryDataContract)
                   {
                       var categoryValue = new CATEGORY_VALUE
                       {
                           categoryid = iD,
                           languagecode = item.Languagecode,
                           name = item.Name,
                           tag = item.Tag,
                           keyword = item.Keyword,
                           decription = item.Decription,
                       };
                       _categoryValueBo.Add(categoryValue);
                   }
               }
               scope.Complete();
               return iD;
           }
       }
       #endregion

       #region Update
       public Boolean Update(List<RelaxCategoryDataContract> categoryDataContract)
       {
           using (var scope = new TransactionScope())
           {
               var category = new CATEGORY_REF
               {
                   id = Convert.ToInt32(categoryDataContract[0].Id),
                   typeid = Convert.ToInt32(categoryDataContract[0].Type),
                   ord = Convert.ToInt32(categoryDataContract[0].Ord),
                   active = Convert.ToInt32(categoryDataContract[0].Active)
               };
               _categoryBo.Update(category);
              
               foreach (var item in categoryDataContract)
               {
                   var categoryValue = new CATEGORY_VALUE
                   {
                       categoryid = Convert.ToInt32(item.Id),
                       languagecode = item.Languagecode,
                       name = item.Name,
                       tag = item.Tag,
                       keyword = item.Keyword,
                       decription = item.Decription,
                   };
                   if (!_categoryValueBo.Update(categoryValue))
                   {
                       _categoryValueBo.Add(categoryValue);
                   }
               }
               scope.Complete();
               return true;
           }
       }
       #endregion

       #region Delete

       public Boolean Delete(List<long> lId, long iLanguageCode)
       {
           using (var scope = new TransactionScope())
           {
               foreach (var id in lId)
               {
                   _categoryValueBo.Delete(id, iLanguageCode);
               }
               scope.Complete();
               return true;
           }
       }
       #endregion
   }
}
